const Component = React.Component;

class ToDoItem extends React.Component {
    delete() {
        this.props.deleteItem(this.props.data.id);
    }
    complete() {
        this.props.okItem(this.props.data.id);
    }
    render() {

        let data = this.props.data;

        return (
            <tr >
                <td className='align-middle'>{data.text}</td>
                <td className='align-middle'>{data.time}</td>
                <td className='align-middle'>{data.done == 0 ? "未完成" : "完成"}</td>
                <td>
                    <a className="btn btn-primary" onClick={this.delete.bind(this)}>删除</a>
                    <a className="btn btn-success" onClick={this.complete.bind(this)}>
                        <span className="glyphicon glyphicon-ok" aria-hidden="true"></span>
                        完成
                   </a>
                </td>
            </tr>
        );
    }
}

class ToDoList extends React.Component {
    render() {
        let todos = this.props.data;
        let todoItems = todos.map(item => {
            return <ToDoItem okItem={this.props.okItem} deleteItem={this.props.deleteItem} key={item.id} data={item} />
        });

        return (
            <table className="table table-sm">
                <thead>
                    <tr>
                        <th>内容</th>
                        <th>时间</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {todoItems}
                </tbody>
            </table>
        );
    }
}

class ToDoapp extends React.Component {
    constructor(props) {
        super(props);
        this.ids = 1;
        this.state = {
            todos: []
        };
        this.addItem = this.addItem.bind(this);
        this.deleteItem = this.deleteItem.bind(this);
        this.okItem = this.okItem.bind(this);
    }
    okItem(id) {
        this.state.todos.map(item => {
            if (item.id == id) {
                item.done = 1;
            }
            return item;
        });
        this.setState({
            todos: this.state.todos
        });
    }
    deleteItem(id) {
        let newtodos = this.state.todos.filter((item) => {
            return !(item.id == id)
        });
        this.setState({
            todos: newtodos
        });
    }

    addItem(value) {
        this.state.todos.unshift(
            {
                id: this.ids++,
                text: value,
                time: (new Date()).toLocaleString(),
                done: 0
            }
        )

        this.setState({
            todos: this.state.todos
        });
    }

    render() {
        return (
            <div className="container">
                <br />
                <br />
                <br />
                <div className="panel panel-default">
                    <div className="panel-headingbg-danger">
                        <h1 className="text-center ">ToDo<small>你要做什么？</small></h1>
                        <hr />
                    </div>
                    <div className="panel-body">
                        <ToDoForm addItem={this.addItem} />
                        <ToDoList okItem={this.okItem} deleteItem={this.deleteItem} data={this.state.todos} />
                    </div>
                </div>
            </div>
        );
    }
}

class ToDoForm extends React.Component {
    tijiao(event) {
        event.preventDefault();
    }
    add(event) {

        if (event.type == "keyup" && event.keyCode != 13) {
            return false;
        }

        let txt = this.refs.txt.value;
        if (txt == "") return false;
        this.props.addItem(txt);
        this.refs.txt.value = "";
    }
    render() {
        return (
            <form className="form-horizontal" onSubmit={this.tijiao.bind(this)}>
                <div className="form-group">
                    <div className="col-sm-10">
                        <input ref="txt" type="text" className="form-control" onKeyUp={this.add.bind(this)} id="exampleInputName2" placeholder="请输入内容" />
                    </div>
                    <div className="col-sm-2">
                        <button type="button" className="btn btn-primary" onClick={this.add.bind(this)}>添加</button>
                    </div>
                </div>
            </form>
        );
    }
}

ReactDOM.render(<ToDoapp />, document.getElementById('root'));